package com.bnyy.medicalHousekeeper.moudle.wallet.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.moudle.wallet.adapter.BankCardAdapter;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.wallet.bean.BankCardInfo;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

import static com.bnyy.common.Constant.ActivityRequestCode.ADD_BANK_CARD;

public class BankCardListActivity extends BaseActivityImpl {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private BankCardAdapter adapter;
    private ArrayList<BankCardInfo> bankCardInfos;
    
    public static void show(Context context, ArrayList<BankCardInfo> bankCardInfos) {
        Intent intent = new Intent(context, BankCardListActivity.class);
        intent.putExtra("bankCardInfos", bankCardInfos);
        context.startActivity(intent);
    }
    
    @Override
    public int getLayoutId() {
        return R.layout.activity_bank_card_list;
    }

    @Override
    public String getTitleStr() {
        return "选择银行卡";
    }

    @Override
    public OnRefreshListener registerOnRefreshListener() {
        return new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                requestManager.request(requestManager.mJavaRetrofitService.getBankCardList(),
                        new BaseObserverImpl<ArrayList<BankCardInfo>>(mRefreshLayout) {
                            @Override
                            public void onSuccess(ArrayList<BankCardInfo> bankCardInfos) {
                                super.onSuccess(bankCardInfos);
                                BankCardListActivity.this.bankCardInfos = bankCardInfos;
                                adapter.refresh(bankCardInfos);
                            }
                        });
            }
        };
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new BankCardAdapter(mContext, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BankCardInfo bankCardInfo = (BankCardInfo) v.getTag();
                Intent intent = new Intent();
                intent.putExtra("bankCardInfo", bankCardInfo);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        recyclerView.setAdapter(adapter);
        mRefreshLayout.autoRefresh(300);
    }

    @OnClick(R.id.tv_add_bank_card)
    public void onViewClicked() {
        Intent intent = new Intent(mContext, AddBankCardActivity.class);
        startActivityForResult(intent, ADD_BANK_CARD);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ADD_BANK_CARD && resultCode == RESULT_OK) {
            BankCardInfo bankCardInfo = (BankCardInfo) data.getSerializableExtra("bankCardInfo");
            bankCardInfos.add(bankCardInfo);
            adapter.refresh(bankCardInfos);
//            mRefreshLayout.autoRefresh();
        }
    }
}




































